package com.witmore.neutron.ai.web.controller;

import com.jk.aliyun.oss.JkOssClientCredentials;
import com.jk.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传接口
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class OSSFileController {

    @Autowired
    private JkOssClientCredentials jkOssClient;

    /**
     * 获取 OSS 上传所需的临时凭证
     */
    @GetMapping(value = "/getUploadCredentials", name = "获取 OSS 上传所需的临时凭证")
    public ResponseResult<Map<String, String>> getUploadCredentials(
            @RequestParam("prefix") String prefix) {
        if (prefix == null || prefix.isEmpty()) {
            return ResponseResult.fail(1001, "文件路径不能为空");
        }
        Map<String, String> uploadCredentials = new HashMap<>();
        try {
            uploadCredentials = jkOssClient.getUploadCredentials(prefix);
        } catch (Exception e) {
            // 处理异常，返回错误信息给前端
            uploadCredentials.put("error", "获取临时凭证失败: " + e.getLocalizedMessage());
        }
        return ResponseResult.success(uploadCredentials);
    }

}
